package com.cloudmusic.constant;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.util.ResourceUtils;

/**
 * 系统变量 env.properties文件的参数配置在这里获取
 * @author you
 * @since 2018-1-17
 */
public class EnvConfig{
	
	private static Map<String, Object> config=new HashMap<String, Object>();
	private static Logger log=Logger.getLogger(EnvConfig.class);
	static {
		InputStream in=null;
		try {
//			File file=new File(Thread.currentThread().getContextClassLoader().getResource("/").getPath()
//					+"config.properties");
			File file = ResourceUtils.getFile("classpath:config.properties");
			if(!file.exists()){
				log.warn("config file : 'env.properties' dose not exists,please check your path or make sure you doesn't need it");
			}
			in=new FileInputStream(file);
			Properties properties=new Properties();
			properties.load(in);
			log.debug("配置是否空？"+properties.isEmpty());
			Iterator<Object> iterator = properties.keySet().iterator();
			while (iterator.hasNext()) {
				String key = (String) iterator.next();
				config.put(key, properties.get(key));
			}
			log.info("加载系统配置文件完成！");
		} catch (Exception e) {
			log.error("加载系统配置文件失败", e);
		}finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					log.error("关闭输入流错误", e);
				}
			}
		}
	}
	/**
	 * 根据Key获取value
	 * @param key
	 * @return
	 */
	public static Object get(String key){
		return config.get(key);
	}
	public static String getString(String key){
		return (String) config.get(key);
	}
	public static int getInt(String key){
		return Integer.valueOf((String) config.get(key));
	}
	public static double getDouble(String key){
		return Double.valueOf((String) config.get(key));
	}
	public static Map<String, Object> getAll(){
		return config;
	}
	
}


//获取对象方法private Logger logger=Logger.getLogger(xxx.class);
//logger.info("user"+str1+"  "+str2)可以替代syso
//logger.error("",e);输出错误信息
